/**
 * API 服务基类类型定义
 * 用于替代 api/base/index.ts 中的 any 类型
 */

import type {
  PageRequest,
  PageResponse,
  ApiResponse,
  DropdownResponse,
} from './index';

/**
 * 文件对象类型
 */
export interface FileObject {
  /** 文件 */
  file: File;
  /** 文件名 */
  name: string;
  /** 文件大小 */
  size: number;
  /** MIME类型 */
  type: string;
}

/**
 * 上传参数
 */
export interface UploadParams {
  /** 文件 */
  file?: File;
  /** 其他参数 */
  [key: string]: any;
}

/**
 * 通用列表请求参数
 */
export type ListParams = Record<string, any>;

/**
 * 通用详情响应
 */
export type DetailResponse<T = any> = T;

/**
 * 导出参数
 */
export interface ExportParams extends PageRequest {
  /** 选中的key列表 */
  selectKeyList?: (number | string)[];
}

/**
 * 导入响应
 */
export interface ImportResponse {
  /** 导入成功数 */
  successCount: number;
  /** 导入失败数 */
  failCount: number;
  /** 错误信息 */
  errors?: string[];
}

/**
 * 取消令牌接口
 */
export interface CancelToken {
  promise: Promise<void>;
  reason?: string;
  throwIfRequested(): void;
}

/**
 * 基础API服务方法参数类型
 */
export interface BaseApiMethods {
  /** 列表查询 */
  list: <T = any>(data: ListParams, cancel?: boolean | CancelToken) => Promise<ApiResponse<T[]>>;

  /** 分页查询 */
  page: <T = any>(
    data: PageRequest,
    cancel?: boolean | CancelToken
  ) => Promise<ApiResponse<PageResponse<T>>>;

  /** 详情 */
  detail: <T = any>(id: number | string, cancel?: boolean | CancelToken) => Promise<ApiResponse<T>>;

  /** 下拉列表数据 */
  dropdownData: (data?: ListParams, cancel?: boolean | CancelToken) => Promise<ApiResponse<DropdownResponse>>;

  /** 新增 */
  add: <T = any>(data: T, cancel?: boolean | CancelToken) => Promise<ApiResponse<{ id: number }>>;

  /** 更新 */
  update: <T = any>(data: T, cancel?: boolean | CancelToken) => Promise<ApiResponse<null>>;

  /** 设置状态 */
  setStatus: (data: { id: number | string; status: number }, cancel?: boolean | CancelToken) => Promise<ApiResponse<null>>;

  /** 删除 */
  delete: (data: { id: number | string }, cancel?: boolean | CancelToken) => Promise<ApiResponse<null>>;

  /** 批量删除 */
  batchDelete: (data: { ids: (number | string)[] }, cancel?: boolean | CancelToken) => Promise<ApiResponse<null>>;

  /** 导出 */
  exportData: (data: ExportParams, cancel?: boolean | CancelToken) => Promise<Blob>;

  /** 导入 */
  importData: (file: File, cancel?: boolean | CancelToken) => Promise<ApiResponse<ImportResponse>>;

  /** 上传文件 */
  uploadFile: (
    params: UploadParams,
    action: string,
    cancel?: boolean | CancelToken
  ) => Promise<ApiResponse<{ url: string; name: string }>>;
}

/**
 * 动态API客户端接口
 */
export interface DynamicApiClient {
  /** 基础方法 */
  [key: string]: any;

  /** 通用方法 */
  list: <T = any>(data: ListParams, cancel?: boolean | CancelToken) => Promise<ApiResponse<T[]>>;
  page: <T = any>(
    data: PageRequest,
    cancel?: boolean | CancelToken
  ) => Promise<ApiResponse<PageResponse<T>>>;
  detail: <T = any>(id: number | string, cancel?: boolean | CancelToken) => Promise<ApiResponse<T>>;
  dropdownData: (data?: ListParams, cancel?: boolean | CancelToken) => Promise<ApiResponse<DropdownResponse>>;
  add: <T = any>(data: T, cancel?: boolean | CancelToken) => Promise<ApiResponse<{ id: number }>>;
  update: <T = any>(data: T, cancel?: boolean | CancelToken) => Promise<ApiResponse<null>>;
  delete: (data: { id: number | string }, cancel?: boolean | CancelToken) => Promise<ApiResponse<null>>;
  batchDelete: (data: { ids: (number | string)[] }, cancel?: boolean | CancelToken) => Promise<ApiResponse<null>>;
  exportData: (data: ExportParams, cancel?: boolean | CancelToken) => Promise<Blob>;
  importData: (file: File, cancel?: boolean | CancelToken) => Promise<ApiResponse<ImportResponse>>;
  uploadFile: (
    params: UploadParams,
    action: string,
    cancel?: boolean | CancelToken
  ) => Promise<ApiResponse<{ url: string; name: string }>>;
}

/**
 * API工厂函数类型
 */
export type ApiFactory<T extends DynamicApiClient = DynamicApiClient> = (
  baseUrl: string
) => T;
